package com.amazon.spring8amazon.common;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author cep123
 * @date 2024/9/6 10:40
 */
public class QinWordsDelete {


    public static void main(String[] args) throws Exception {
        String str = "REFLEX SERIES Anti Conductor Baton Mahogany Handle Fiberglass Stick for Choir Music Conducting WandMusician Baton,Music Conductor Batonfor Choir, Orchestra, Band Concert with Comfortable GripConducting Batons";
        // 首字母全部小写
        str = str.toLowerCase();

        // 去除空格 注意数据提前转换成小写
        String[] qinWords = {"diving,","anti", "bcd", "100%", "suit","ctr360", "Curly Tail" ,"div", "THE BEATLES", "REFLEX SERIES"};
        List<String> resSingleWord = new ArrayList<>();
        List<String> resMultiWord = new ArrayList<>();
        for (String word : qinWords) {
            if (word.split(" ").length >1){
                resMultiWord.add(word.toLowerCase());
            } else {
                resSingleWord.add(word.toLowerCase());
            }

        }

        // 单个单词的情况
        Map<String, Object> res1 = containsSingleWords(str, resSingleWord);
        // 多个单词组成词组的检测
        assert res1 != null;
        Map<String, Object> res2 = containsMultiWords(res1.get("Result").toString(), resMultiWord);
        // 首字母大写
        //System.out.println(res2);
    }

    // Check if a string contains all the words in an array of words



    /**
     *  检测字符串中是否含有某个单词 词组无法检测
     * @param inputString 需要检测的字符串
     * @param wordsList 需要被检测的单词构成的数组
     * @return 返回Map 对象
     */
    public static Map<String, Object> containsSingleWords(String inputString, List<String> wordsList) {
        try {

            // 首字母全部小写
            // inputString 转换为List并删除空格
            List<String> inputStringList = Arrays.asList(
                    inputString
                            .toLowerCase()
                            .replaceAll(" {2}", " ")
                            .replaceAll("\n", " ")
                            .split(" "));
            System.out.println(inputStringList);

            // 打印包含的单词是什么
            String containsWords = Arrays.stream(wordsList.stream().filter(inputStringList::contains).toArray(String[]::new)).reduce((a, b) -> a + "," + b).orElse("");
            // 包含单词转换为List
            List<String> containsWordsList = Arrays.asList(containsWords.split(" "));

            System.out.println("SingleContainsWords: " + containsWords);

            // 使用StringBuilder来构建新的句子
            StringBuilder updatedSentence = new StringBuilder();


            // 判断是否包含所有单词
            if (wordsList.stream().anyMatch(inputStringList::contains)) {

                // 遍历句子的每个单词
                for (String word : inputStringList) {
                    // 去掉标点符号再匹配
//                    String cleanWord = word.replaceAll("[^a-zA-Z0-9%]", "");
                    String cleanWord = word.replaceAll("", "");
                    // 如果当前单词不在要删除的列表中，则将其添加到新的句子中 匹配出来后的再执行遍历删除
                    if (!containsWordsList.contains(cleanWord)) {
                        updatedSentence.append(word).append(" ");
                    }

                }

            } else {
                updatedSentence.append(inputString);

            }

            // 定于一个Map用来存放结果
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("SingleContainsWords", containsWords);
            resultMap.put("Result", updatedSentence.toString().trim());

            return resultMap;
        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
            return null;
        }

    }




    public static Map<String, Object> containsMultiWords(String inputString, List<String> removeWords) throws Exception {

        try {
            StringBuilder regexBuilder = new StringBuilder();
            for (int i = 0; i < removeWords.size(); i++) {
                if (i > 0) {
                    regexBuilder.append("|");
                }
                regexBuilder.append(Pattern.quote(removeWords.get(i)));
            }
            String regex = regexBuilder.toString();

            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(" " + inputString + " ");
            // 打印匹配到的内容
            StringBuilder multiMatcherWords = new StringBuilder();
            if (matcher.find()) {
                multiMatcherWords.append(matcher.group()).append(",");
            }


            String result = matcher.replaceAll("");
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("Result", result.trim());
            resultMap.put("containsWords",multiMatcherWords);
            return resultMap;
        } catch (Exception e) {
            System.out.println("Exception: " + "[containsMultiWords]" + e.getMessage());
            return null;
        }
    }




    //
    public static String firstCharToUpperCase (String inputString) throws Exception {
        try {
            // updatedSentence 调用函数使得每一个单词的首字母大写
            StringBuilder strSentence = new StringBuilder();
            String[] arr = inputString.split(" ");
            for (String s : arr) {
                strSentence.append(s.substring(0, 1).toUpperCase()).append(s.substring(1)).append(" ");
            }
            return strSentence.toString().trim();
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        }
    }



}
